package com.crawler;

import com.crawler.entity.WuLing;
import com.crawler.utils.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author : gelianbin
 * @createDate : 5/12/2021
 */
@Component
public class EpcDemo {

    @Value("${car.wuling.url}")
    private String url;
    int m= 1;

    @Autowired
    private HttpUtils httpUtils;

    //当下载任务完成后，间隔多长时间进行下一次的任务。
    @Scheduled(fixedDelay =100*1000)
    public void epcTask() {
        //需要解析的地址（五菱）
//        String url ="https://car.autohome.com.cn/price/brand-114-59.html";

                   //https://car.autohome.com.cn/price/series-5714.html#pvareaid=2042207
        //解析页面
        String html = httpUtils.doGetHtml(url);
//        汽车之家车系编码：5881z
//        汽车之家车系名称：   <a href ="https://www.autohome.com.cn/5881/#pvareaid=6834146" class="font-bold">五菱征途</a>
//        汽车之家车型名称：2021款 马卡龙时尚款 <div class="interval01-list-cars-infor"> 2021款 马卡龙时尚款
        Document doc = Jsoup.parse(html);
//        Element s5174 = doc.getElementById("s5174");
//        System.out.println(s5174);
        //获取汽车之家车系名称：<div class="main-title">  <a href ="https://www.autohome.com.cn/5881/#pvareaid=6834146" class="font-bold">五菱征途</a>
        Elements elementsByClass = doc.getElementsByClass("font-bold");
        List wulingList = new ArrayList<WuLing>();
        for (int i = 0; i < elementsByClass.size(); i++) {
            //车系名称
            String chexi = elementsByClass.get(i).text();

            System.out.println(chexi);
            //从元素中获取属性值
            String href = elementsByClass.get(i).attr("href");
            System.out.println(href);
            //从href中获取编号
            String[] split = href.split("/");
            //车型编号
            System.out.println(split[3]);
            StringBuilder l_url = new StringBuilder("https://car.autohome.com.cn/price/series-");
            l_url.append(split[3] + ".html#pvareaid=2042207");
            //解析页面
            String l_html = httpUtils.doGetHtml(l_url.toString());
//            System.out.println(l_url.toString());
            Document l_doc = Jsoup.parse(l_html);
            Elements l_first = l_doc.select("div.interval01-list-cars > div > p:eq(0)");
//            System.out.println(l_first);
            for (int j = 0; j < l_first.size(); j++) {
                //车系
                WuLing wuLing = new WuLing();
                //车系编号
                wuLing.setCarSeriesNumber(Long.parseLong(split[3]));
                //车系名称
                wuLing.setCarSeriesName(chexi);
//                System.out.println(l_first.text());
                String[] s_p = l_first.get(j).text().split(" ");
                //2020款 1.2L 厢式运输车实用型 国VI 2座 LSI
                if (s_p.length >= 1){
                    String year = s_p[0];
                    //车型年款
                    wuLing.setCarModelYear(year);
                    System.out.println(year);
//                    s_p[0] = "";不要年款
                    String join = StringUtils.join(s_p, "");
                    //车型名称
                    wuLing.setCarModelName(join);
                    System.out.println(join);

                    wulingList.add(wuLing);
                }
                m++;
            }
        }
        System.out.println(m);

        //-------------------------------------------
        for (int i = 0; i < wulingList.size(); i++) {
            System.out.println(wulingList.get(i));
        }
        System.out.println(wulingList.size());
    }



}
